package com.shengwei.truck.driver.socket;
import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import com.shengwei.truck.driver.view.ui.app.MyApplication;
import com.shengwei.truck.driver.R;
import com.shengwei.truck.driver.utils.MyLogCat;
import com.shengwei.truck.driver.utils.MyLogFile;
import com.shengwei.truck.driver.utils.NotificationUtils;
import com.shengwei.truck.driver.utils.SPUtils;

/**
 * Service服务随操作系统启动而启动，用于启动socket客户端
 */
public class SocketService extends Service {
    public static final String TAG = SocketService.class.getSimpleName();
    private  MyApplication mApp;

    @Override
    public void onCreate() {
        super.onCreate();
        mApp = MyApplication.getInstance();
        MyLogFile.d(TAG,"SocketService启动");
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        MyLogCat.e(TAG, "onStartCommand thread id is " + Thread.currentThread().getId());
        // 启动一个前台服务后再开始定位，用于提高定位保活率
        Notification notification = buildNotification();
        startForeground(100,notification);

        //激活定位服务（因为有时定位服务休眠）：当有正在进行中的任务时（任务状态为1，2），则跟踪司机位置
        String curTaskId = (!TextUtils.isEmpty(MyApplication.mCurTaskId)) ? MyApplication.mCurTaskId : SPUtils.getString(this,"curTaskId");
        MyLogCat.i(TAG,"curTaskId="+curTaskId);
        if(!TextUtils.isEmpty(curTaskId)){
            MyApplication.mLocationService.start();
            MyLogCat.i(TAG,"mLocationService.start()");
        }

        // 启动Socket去连接服务器
        SocketManager socketManager = SocketManager.getInstance(SocketService.this);
        socketManager.connectServer();
        return START_STICKY;
    }
    @Override
    public void onDestroy() {
        SocketManager.getInstance(SocketService.this).clear();
        Log.e(TAG, "onDestroy");
    }
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    /**
     * 构建Notification，以保证后台连续定位
     * @return
     */
    public Notification buildNotification() {
        Notification notification;
        String notifyTitle = this.getResources().getString(R.string.notify_title);
        String notifyContent = this.getResources().getString(R.string.notify_content);
        NotificationUtils mNotificationUtils;
        //设置后台定位
        if (Build.VERSION.SDK_INT >= 26) {  //AndroidO及以上系统
            mNotificationUtils = new NotificationUtils(this);
            Notification.Builder builder2 = mNotificationUtils.getAndroidChannelNotification(notifyTitle, notifyContent);
            builder2.setOnlyAlertOnce(true);
            builder2.setOngoing(true);
            builder2.setSound(null);
            builder2.setVibrate(null);
            builder2.setDefaults(NotificationCompat.FLAG_ONLY_ALERT_ONCE);//取消震动,铃声其他都不好使
            notification = builder2.build();

        } else {
            mNotificationUtils = new NotificationUtils(this,true);
            Notification.Builder builder = mNotificationUtils.getAndroidNotification (notifyTitle, notifyContent);
            notification = builder.build(); // 获取构建好的Notification
        }
        notification.defaults = Notification.DEFAULT_SOUND; //设置为默认的声音
        return notification;
    }
}